# -*- coding:utf8 -*-
# from __future__ import unicode_literals

from humph.base import run_server, rough_route


@rough_route('/hello/', method='GET')
def hello(requst):
    import redis
    r = redis.Redis()
    workers = ['zjm', 'zc', 'yy']
    index = r.get('counter')
    index = int(index) if index else 0
    worker = workers[index % 3]
    return open('hello.html', 'r').read().format(worker=worker)


@rough_route('/test_post/', method='POST')
def test_post(request):
    print 'zjm:', request.post_params
    print request.params
    return 'oh, post~'


@rough_route('/')
def index(requst):
    return 'nothing'


@rough_route('/milk_tea/')
def milk_tea(requst):
    import random
    choices = requst.get_params.get('choices', 'yidiandian,gongcha,happylemon').split(',')
    excludes = requst.get_params.get('excludes', '').split(',')
    choices = [_ for _ in choices if _ not in excludes]
    the_one = random.choice(choices)
    return the_one


tmp_dict = {
    'zjm': set([]),
    'zc': set([]),
    'szy': set([]),
}


@rough_route('/interview/', method='POST')
def interview(requst):
    import redis
    body = requst.post_params
    r = redis.Redis()
    all_cadidates = r.lrange('users', 0, -1)
    candidate = body['candidate']
    for person in all_cadidates:
        if person == candidate:
            return '{} had been talked to {}'.format(r.get(candidate), candidate)
    workers = ['zjm', 'zc', 'yy']
    r.lpush('users', candidate)
    index = r.incr('counter')
    worker = workers[index % len(workers)]
    r.set(candidate, worker)
    return 'next interview: {name} vs {worker}'.format(name=candidate, worker=worker)


if __name__ == "__main__":
    run_server()
